<div class="content content_padding">
  <h1>新增商品</h1>
  
  <% form_for([:admin , @product] , :html => {:class => :form , :multipart => true}) do |f| %>
    <%= f.error_messages %>
          
    <table class='table'>
      <tr>
        <td align="right">    <%= Product.human_attribute_name(:name)%> : </td>
        <td>             <%= f.text_field :name %>    </td>
      </tr>
      <tr>
        <td align="right">      <%= Product.human_attribute_name(:serial)%> : </td>
        <td>             <%= f.text_field :serial %> </td>
      </tr>
      <tr>
        <td align="right">           <%= Product.human_attribute_name(:proved_serial)%> : </td>
        <td>                <%= f.text_field :proved_serial %> </td>
      </tr>
      <tr>
        <td align="right">  <%= Product.human_attribute_name(:sort_id)%>  :</td>
        <td>  <%= f.select :sort_id ,Sort.all.map{|s| [s.name,s.id]}  %> </td>
      </tr>
      <tr>
        <td align="right">  <%= Product.human_attribute_name(:color_id)%>  :</td>
        <td>   <%= f.select :color_id , Color.all.map{|c| [c.name ,c.id]} %></td>
      </tr>
      <tr>
        <td align="right">  <%= Product.human_attribute_name(:state)%>  :</td>
        <td>   <%= f.select :state , Product::STATE.map { |k,v|[v ,k]  } %></td>
      </tr>
      <tr>
        <td  align="right">  <%= Product.human_attribute_name(:rank)%> :</td>
        <td>    <%= f.select :rank , Product::RANK.map { |k,v|[v ,k]  } %></td>
      </tr>
      <tr>
        <td  align="right">  <%= Product.human_attribute_name(:price)%> :</td>
        <td>   <%= f.text_field :price %></td>
      </tr>
      <tr>
        <td  align="right"> <%= Product.human_attribute_name(:current_price)%> :</td>
        <td>       <%= f.text_field :current_price %></td>
      </tr>
      <tr>
        <td  align="right"><%= Product.human_attribute_name(:introduction)%> :</td>
        <td>        <%= f.text_area :introduction %></td>
      </tr>
      <tr>
        <td  align="right">      <label for="product_picture_id">选择图像</label> :</td>
        <td>    <p>
          
            <%=  file_field_tag(:image) %>
            <% unless @product.product_images.blank? %>
              <% @product.product_images.each do |picture| %>
                <div class="file_list" id="picture_< %= picture.id %>">
                  <ul>
                    <li>
                      文件: <%= picture.image_file_name %>
                      <%= link_to_remote "删除图片",:url=>delete_product_picture_ajax_path(:p_id=>picture.id),:confirm=>"是否要删除该产品图片?",:method=>:get%>
                    </li>
                  </ul>
                </div>
              <% end %>
            <% end %>
        </p></td>
      </tr>
    
      <tr>
        <td  align="right">&nbsp;</td>
        <td>           <%= f.submit "新增"  , :class => :button %></td>
      </tr>
    </table>
  <% end %>
  
  <%= link_to '返回', products_path %>
          
  <script type="text/javascript">
      var multiple_upload_attachment_counter = 0;
      multiple_upload_attachment($("image"), 100);
      function multiple_upload_attachment(ele,max_size)
      {
          Event.observe(ele,'change',function(){
              addAttachmentDiv(ele,max_size);
          }
      );
          if(multiple_upload_attachment_counter>=max_size)ele.disabled=true;
      }
      function addAttachmentDiv(ele, max_size)
      {
          if(!$A(['jpg','bmp','png','gif','rar','zip','tar','gz','jar','war']).any(function(extName){return new RegExp('\\.'+extName+'$','i').test(ele.value);})){
              alert("文件类型错误！");
              return false;
          }
          multiple_upload_attachment_counter++;
          var div=new Element("div").update("<ul><li>文件: "+ele.value+" <a href='#' onclick='removeAttachmentDiv(this, \""+ele.id+"\");return false;'>删除</a></li></ul>");
          div.addClassName("file_list")
          var new_input=new Element("input",{type:"file",name:ele.name,id:ele.id,disabled:multiple_upload_attachment_counter>=max_size});
          Event.observe(new_input,'change',function(){
              addAttachmentDiv(new_input,max_size);
          }
      );
          ele.insert({before:new_input,after:div});
          ele.id=ele.id+multiple_upload_attachment_counter;
          ele.name="image[]";
          div.appendChild(ele.hide().remove());
      }
      function removeAttachmentDiv(link,eleId){
          multiple_upload_attachment_counter--;
          link.parentNode.parentNode.parentNode.remove();
          $(eleId).disabled=false;
      }
  </script>    
</div>